﻿// Copyright 2010 Maxime Serrano <lastchronicler@gmail.com>
using System;
using System.Net;

namespace MCSharp {
    static class EnvironmentCommands {
        internal static void Init() {
            CommandList.RegisterCommand(cdTime);
            CommandList.RegisterCommand(cdTimeLock);
            CommandList.RegisterCommand(cdSeed);
            CommandList.RegisterCommand(cdSetSpawn);
        }

        static CommandDescriptor cdTime = new CommandDescriptor {
            name = "time",
            consoleSafe = true,
            permissions = new Permission[] { Permission.EditTime },
            usage = "/time <time>",
            help = "Sets the sun's position (time) to the number you specify.",
            handler = cmdSetTime
        };

        internal static void cmdSetTime(Player player, Command cmd) {
            string msg = cmd.NextAll().Trim();
            if (msg != null && msg.Length > 0) {
                long timeCode;
                try {
                    timeCode = long.Parse(msg);
                } catch {
                    player.Message(Color.Announcement + "You must enter a valid timecode!");
                    return;
                }
                Server.SendToAll(Color.Sys + player.nick + " set the world's time to " + msg);
                Server.world.time = timeCode;
                Server.SendToAll(PacketWriter.MakeTimeUpdate(timeCode));
            } else {
                player.Message(Color.Announcement + "The current server time is " + Server.world.time);
                player.Message(Color.Announcement + "To change the time, type " + Color.Help + "/time <time>");
                return;
            }
        }

        static CommandDescriptor cdTimeLock = new CommandDescriptor {
            name = "timelock",
            consoleSafe = true,
            permissions = new Permission[] { Permission.EditTime },
            usage = "/timelock",
            help = "Toggles time lock. If locked, time will not progress on the server. Saved and editable in config.",
            handler = cmdTimeLock
        };

        internal static void cmdTimeLock(Player player, Command cmd) {
            if (Config.GetBool(ConfigKey.AllowTime)) {
                Config.SetValue(ConfigKey.AllowTime, false);
                Server.SendToAll(player.info.playerClass.color + player.nick + Color.Sys + " has locked time. Time will no longer progress.");
            } else {
                Config.SetValue(ConfigKey.AllowTime, true);
                Server.SendToAll(player.info.playerClass.color + player.nick + Color.Sys + " has unlocked time. Time will now progress.");
            }
            Config.Save();
        }

        static CommandDescriptor cdSeed = new CommandDescriptor {
            name = "seed",
            consoleSafe = true,
            usage = "/seed",
            help = "Tells you the seed of the current MySMP map, allowing you to re-create the terrain on another server.",
            handler = cmdSeed
        };

        internal static void cmdSeed(Player player, Command cmd) {
            player.Message(Color.Announcement + "The current world's seed is: " + Server.world.randomSeed);
        }

        static CommandDescriptor cdSetSpawn = new CommandDescriptor {
            name = "setspawn",
            consoleSafe = false,
            permissions = new Permission[] { Permission.SetSpawn },
            usage = "/setspawn",
            help = "Sets your current position as the spawn position of the map.",
            handler = cmdSetSpawn
        };

        internal static void cmdSetSpawn(Player player, Command cmd) {
            Server.world.spawn = player.pos;
            Server.world.spawn.y += 2;
            Server.world.SaveWorld();
            Server.SendToAll(player.info.playerClass.color + player.name + Color.Sys + " has updated the map's spawn position.");
        }
    }
}
